package com.example.myapplication.ui.home;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class CategoryListActivity extends AppCompatActivity {

    private RecyclerView courseRecyclerView;
    private List<Course> categoryCourses = new ArrayList<>();
    private TextView categoryTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_list);

        // 获取传递的分类信息
        String categoryId = getIntent().getStringExtra("categoryId");
        String categoryName = getIntent().getStringExtra("categoryName");

        // 设置默认值，防止为null
        if (categoryId == null) {
            categoryId = "1";
        }
        if (categoryName == null) {
            categoryName = "编程";
        }

        initViews();
        loadCoursesByCategory(categoryId, categoryName);
    }

    private void initViews() {
        courseRecyclerView = findViewById(R.id.categoryCourseList);
        categoryTitle = findViewById(R.id.categoryTitle);

        // 2列网格布局
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        courseRecyclerView.setLayoutManager(layoutManager);

        // 返回按钮
        TextView btnBack = findViewById(R.id.btnBack);  // 这里改成TextView类型
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); // 关闭页面返回
            }
        });
    }

    private void loadCoursesByCategory(String categoryId, String categoryName) {
        // 设置标题
        categoryTitle.setText(categoryName + "课程");

        // 清空原有数据
        categoryCourses.clear();

        // 根据分类ID加载对应的课程数据（保持不变）
        switch (categoryId) {
            case "1": // 编程
                categoryCourses.add(new Course("1", "https://img95.699pic.com/xsj/10/1v/5k.jpg",
                        "Android开发入门", "张老师", "初级", 4.8f, ""));
                categoryCourses.add(new Course("2", "https://img95.699pic.com/xsj/07/2c/8i.jpg",
                        "Java基础实战", "李老师", "入门", 4.6f, ""));
                categoryCourses.add(new Course("3", "https://img95.699pic.com/xsj/01/9s/3b.jpg",
                        "Python入门到精通", "赵老师", "初级", 4.7f, ""));
                categoryCourses.add(new Course("4", "https://img95.699pic.com/xsj/0m/5b/2g.jpg",
                        "Swift编程开发", "陈老师", "中级", 4.9f, ""));
                break;
            case "2": // 设计
                categoryCourses.add(new Course("5", "https://img95.699pic.com/xsj/0s/6b/2b.jpg",
                        "UI设计原理", "王老师", "中级", 4.9f, ""));
                categoryCourses.add(new Course("6", "https://img95.699pic.com/xsj/0r/3p/8n.jpg",
                        "用户体验设计", "刘老师", "高级", 4.8f, ""));
                break;
            // 其他分类...
            default:
                categoryCourses.add(new Course("1", "https://img95.699pic.com/xsj/10/1v/5k.jpg",
                        "Android开发入门", "张老师", "初级", 4.8f, ""));
                break;
        }

        // 关键：创建适配器并绑定到RecyclerView
        CategoryCourseAdapter adapter = new CategoryCourseAdapter(this, categoryCourses);
        courseRecyclerView.setAdapter(adapter);
    }
}